package com.zhongke.common.widget.statusview;


import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhongke.common.R;
import com.zhongke.common.widget.ZKLoadingView;


/**
 * 自定义要切换的布局，通过ZKStatusViewHelper实现真正的切换
 * 使用者可以根据自己的需求，使用自己定义的布局样式
 *
 * @author 陈元旺
 */
public class ZKLoadViewHelper {

    private ZKIStatusViewHelper helper;
    /**
     * 提示界面整体布局
     */
    private View statusViewLayout;
    /**
     * 界面图片控件
     */
    private ImageView ivNoData, ivError;

    private TextView btnRefresh, tvNoData, tvError;
    private View viewError, viewEmpty;
    private ZKLoadingView viewLoad;
    private FrameLayout flHelper;


    public ZKLoadViewHelper(ZKIStatusViewHelper helper) {
        super();
        this.helper = helper;
        statusViewLayout = helper.inflate(R.layout.module_status_view_layout);
        flHelper = statusViewLayout.findViewById(R.id.fl_load_err);
        viewLoad = statusViewLayout.findViewById(R.id.iv_loading);
        viewError = statusViewLayout.findViewById(R.id.ll_load_failure);
        viewEmpty = statusViewLayout.findViewById(R.id.fl_no_data);
        ivNoData = statusViewLayout.findViewById(R.id.iv_no_data);
        ivError = statusViewLayout.findViewById(R.id.ivError);
        btnRefresh = statusViewLayout.findViewById(R.id.btn_reload);
        tvNoData = statusViewLayout.findViewById(R.id.tv_no_data);
        tvError = statusViewLayout.findViewById(R.id.reason);

    }


    /**
     * 数据为空时要显示的布局
     *
     * @param message：提示消息
     */
    public void showEmpty(String message, int imageId) {
        ivNoData.setImageResource(imageId);
        tvNoData.setText(message);
        viewLoad.setVisibility(View.GONE);
        viewError.setVisibility(View.GONE);
        viewEmpty.setVisibility(View.VISIBLE);
        viewLoad.dismiss();
        helper.showLayout(flHelper);
    }


    /**
     * 数据出错的布局
     *
     * @param message：提示消息
     */
    public void showErrorNoButton(String message, int imageId) {
        ivError.setImageResource(imageId);
        tvError.setText(message);
        btnRefresh.setVisibility(View.GONE);
        viewLoad.setVisibility(View.GONE);
        viewError.setVisibility(View.VISIBLE);
        viewEmpty.setVisibility(View.GONE);
        viewLoad.dismiss();
        helper.showLayout(flHelper);
    }

    /**
     * 数据出错时要显示的布局
     *
     * @param listener:按钮点击事件
     * @param message：提示消息
     * @param buttonText：按钮文本
     */
    public void showError(OnClickListener listener, String message, String buttonText, int imageId) {

        ivError.setImageResource(imageId);
        if (!TextUtils.isEmpty(message)) {
            tvError.setText(message);
        }
        btnRefresh.setText(buttonText);
        btnRefresh.setOnClickListener(listener);
        viewLoad.setVisibility(View.GONE);
        viewError.setVisibility(View.VISIBLE);
        viewEmpty.setVisibility(View.GONE);
        viewLoad.dismiss();
        helper.showLayout(flHelper);
    }


    public void showLoading() {
        viewLoad.setVisibility(View.VISIBLE);
        viewError.setVisibility(View.GONE);
        viewEmpty.setVisibility(View.GONE);
        viewLoad.show();
        helper.showLayout(flHelper);
    }

    // loading背景变透明
    public void showLoadingTransparent() {
        viewLoad.setVisibility(View.VISIBLE);
        flHelper.setBackground(null);
        flHelper.setBackgroundResource(R.color.transparent);
        viewError.setVisibility(View.GONE);
        viewEmpty.setVisibility(View.GONE);
        viewLoad.show();
        helper.showLayout(flHelper);
    }


    public void restore() {
        viewLoad.dismiss();
        helper.restoreView();
    }
}
